<?php
/*******************************************************************************
requestDisplayer.class.php


Created by Jason White (jbwhite@emory.edu)
Modified by NCSU Libraries, NC State University. Modifications by Karl Doerr and Troy Hurteau (libraries.opensource@ncsu.edu).

This file is part of NCSU's distribution of ReservesDirect. This version has not been downloaded from Emory University
or the original developers of ReservesDirect. Neither Emory University nor the original developers of ReservesDirect have authorized
or otherwise endorsed or approved this distribution of the software.

Copyright (c) 2004-2006 Emory University, Atlanta, Georgia.

Licensed under the NCSU ReservesDirect License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. You may obtain a copy of the full License at
 http://www.lib.ncsu.edu/it/opensource/

ReservesDirect is distributed in the hope that it will be useful,
but is distributed "AS IS" and WITHOUT ANY WARRANTY, without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE, and without any warranty as to non-infringement of any third
party's rights. See the License for the specific language governing permissions and limitations under the License.

The original version of ReservesDirect is located at:
http://www.reservesdirect.org/

This version of ReservesDirect, distributed by NCSU, is located at:
http://code.google.com/p/reservesdirect-ncsu/

*******************************************************************************/
require_once(APPLICATION_PATH . '/classes/terms.class.php');
require_once(APPLICATION_PATH . '/classes/circRules.class.php');
require_once(APPLICATION_PATH . '/displayers/noteDisplayer.class.php');
require_once(APPLICATION_PATH . '/managers/ajaxManager.class.php');

require_once(APPLICATION_PATH . '/classes/Queue/Encoding.php');

class requestDisplayer extends noteDisplayer {
	
	protected $_displayerName = 'request';
	
	function displayAllRequest($requestList, $libList, $request, $user, $msg = '')
	{
		$model = array(
			'user' => $user,
			'requestList' => $requestList,
			'libList' => $libList,
			'request' => $request, //#TODO get away from passing the whole request...
			'libraryId' => $request['libraryId'],
			'statusId' => $request['statusId'],
			'sort' => $request['sort'],
			'dir' => $request['dir'],
			'filterType' => $request['type'],
			'msg' => $msg
		);	
		$this->display('displayAll', $model);
	}
	
	function displayAllRequestMobile($requestList, $libList, $request, $user, $msg = '')
	{
		Rd_Layout::setLinks('mobileFooter', array('Return to Course List' => './?cmd=viewCourseList'));
		//#TODO needed for now to get request data down into the print view:
		$model = array(
			'user' => $user,
			'requestList' => $requestList,
			'libList' => $libList,
			'request' => $request,
			'msg' => $msg,
			'libraryId' => $request['libraryId'],
			'statusId' => $request['statusId'],
			'sort' => $request['sort'],
			'dir' => $request['dir'],
			'filterType' => $request['type']
		);	
		$this->display('displayAllMobile', $model);
	}

	function printSelectedRequest($requestList, $libList, $request, $user, $msg = '')
	{		
		$model = array(
			'requestList' => $requestList,
			'libList' => $libList,
			'request' => $request,
			'user' => $user,
			'msg' => $msg
		);
		$this->display('printSelected', $model);
	}
	
	public function createRequest($model = array()){
		$this->display('create', $model);
	}

	public function createRequestConfirm($model = array()){
		$this->display('createConfirm', $model);
	}	
	
	public function displayRequestList($requestList, $request, $user, $currentUnit, $printView = NULL)
	{	
		$catalogName = Rd_Registry::get('root:catalogName');
		$model = array(
			'requestList' => $requestList,
			'request' => $request,
			'dir' => $request['dir'],
			'sort' => $request['sort'],
			'libraryId' => $request['libraryId'],
			'statusId' => $request['statusId'],
			'user' => $user,
			'currentUnit' => $currentUnit,
			'printView' => $printView,
			'catalogName' => $catalogName
		);
		$this->display('requestList', $model);
	}
	
	function addItem($cmd, $item_data = NULL, $hidden_fields = NULL) {
		if (is_array($cmd) && is_null($item_data)) {
			// temporary overloding of this function to accept a model
			$item_data = $cmd['itemData'];
			$hidden_fields = $cmd['propagatedData'];
			$request = $cmd['request'];
			$cmd = $cmd['cmd'];
		} else {
			$request = $_REQUEST; //#TODO deprecate when we refactor the function parameters
		}
		global $g_notetype;
		$catalogName = Rd_Registry::get('root:catalogName');
		$u = Rd_Registry::get('root:userInterface');
		
		//for ease-of-use, define helper vars for determining digital/physical items
		$isPhysical = ($cmd=='addPhysicalItem') ? true : false;
		$isDigital = ($cmd=='addDigitalItem') ? true : false;
		$isVideo = ($cmd=='addVideoItem') ? true : false;
		
		if ($isVideo) {
			$item_data['times_pages2'] = null;
		}
		
		if ($isVideo && !empty($item_data['times_pages'])) {
			$times = explode(" to ", $item_data['times_pages']);
			$item_data['times_pages'] = $times[0];
			$item_data['times_pages2'] = $times[1];
		}
		
		//get array of document types/icons/helper apps (digital items only)
		$doc_types = $isDigital ? $u->getAllDocTypeIcons(TRUE) : null;
		//get array of libraries (physical items only)
		$libraries = $isPhysical ? $u->getLibraries() : null;
		
		//private user
		if (!empty($item_data['selected_owner'])) {
			//get id
			$selected_owner_id = $item_data['selected_owner'];
			$tmpUser = new user($selected_owner_id);
			//get name
			$selected_owner_name = $tmpUser->getName().' ('.$tmpUser->getUsername().')';
			unset($tmpUser);
		} else {
			$selected_owner_id = NULL;
			$selected_owner_name = NULL;
		}
		
		//deal with barcode prefills
		if (!empty($_REQUEST['searchField'])) {
			if ($_REQUEST['searchField'] == 'barcode') {
				$barcode_select = ' selected = "selected"';
				$control_select = '';
				//assume that this index exists
				$barcode_value = $_REQUEST['searchTerm'];
			} else {
				$barcode_select = '';
				$control_select = ' selected = "selected"';
				$barcode_value = (is_array($item_data['physicalCopy']) && !empty($item_data['physicalCopy'])) ? $item_data['physicalCopy'][0]['bar'] : '';
			}
			$search_term = $_REQUEST['searchTerm'];
		} else {
			$barcode_select = ' selected = "selected"';
			$control_select = '';
			$search_term = '';
			if ($isVideo){
				$item_data['physicalCopy'] = '';
			}
			$barcode_value = (is_array($item_data['physicalCopy']) && !empty($item_data['physicalCopy'])) ? $item_data['physicalCopy'][0]['bar'] : '';
		}

		
		//decide if need to add form encoding type
		$model = array(
			'cmd' => $cmd,
			'formEnctype' => ($isDigital || $isVideo) ? ' enctype="multipart/form-data"' : '',
			'isDigital' => $isDigital,
			'isVideo' => $isVideo,
			'isPhysical' => $isPhysical,
			'addTypeSelect' => array(
				'euclid' => $isPhysical && !Rd_Array::keyExistsAndEquals('addType', $_REQUEST, 'PERSONAL') ? ' checked="true"' : '',  //#TODO make this more generic
				'personal' => $isPhysical && Rd_Array::keyExistsAndEquals('addType', $_REQUEST, 'PERSONAL') ? ' checked="true"' : ''
			),
			'itemData' => $item_data,
			'hiddenFields' => $hidden_fields,
			'barcodeSelect' => $barcode_select,
			'controlSelect' => $control_select,
			'searchTerm' => $search_term,
			'catalogName' => $catalogName,
			'selectedOwnerId' => $selected_owner_id,
			'selectedOwnerName' => $selected_owner_name,
			'docTypes' => $doc_types,
			'libraries' => $libraries,
			'noteTypes' => $g_notetype,
			'requestData' => $request
		);

		$this->display('addItemForm', $model);
	}
	
	
	function addSuccessful($ci, $item_id, $reserve_id, $duplicate_link=false, $ils_results='') {
		$ci->getCourseForUser();
?>
		<div class="borders" style="padding:15px; width:50%; margin:auto;">
			<strong>Item was successfully added to </strong><span class="successText"><?php print($ci->course->displayCourseNo()); ?> <?php print($ci->course->getName());?></span>		
<?php	if(!empty($ils_results)):	//show ILS record creation results ?>
				<br />
				<br />
				<div style="margin-left:20px;">
					<strong>ILS query results:</strong>
					<div style="margin-left:20px;">
						<?php print($ils_results); ?>
					</div>
				</div>
<?php	endif; ?>
			<br />
			<ul>
				<li><a href="index.php?cmd=storeRequest&amp;item_id=<?php print($item_id); ?>">Add this item to another class</a></li>				
<?php	if($duplicate_link): ?>
				<li><a href="index.php?cmd=duplicateReserve&amp;reserveID=<?php print($reserve_id); ?>">Duplicate this item and add copy to the same class</a></li>
<?php	endif; ?>				
				<li><a href="index.php?cmd=editClass&ci=<?php print($ci->getCourseInstanceID()); ?>"> Go to class</a></li>
				<li><a href="index.php?cmd=addPhysicalItem">Add another physical item</a></li>
				<li><a href="index.php?cmd=addDigitalItem">Add another electronic item</a></li>
				<li><a href="index.php?cmd=displayRequest">Return to the Requests Queue</a></li>
			</ul>	
		</div>
<?php
	}
	
	
	/**
	 * Displays list of possible CIs for the item
	 *
	 * @param array $all_possible_CIs = array(
						 * 	'rd_requests' => array(ci1-id, ci2-id, ...),
						 * 	'ils_requests => array(
						 * 		user-id1 = array(
						 * 			'requests' => array(ils-request-id1, ils-request-id2, ...),
						 * 			'ci_list' => array(ci1-id, ci2-id, ...)
						 * 		),
						 * 		user-id2 = ...
						 *	)
						 * )
	 * @param array $selected_CIs = array(ci1_id, ci2_id, ...)
	 * @param array $CI_request_matches = array(
						 * 	ci1-id => array(
						 * 		'rd_request' => rd-req-id,
						 * 		'ils_requests' => array(
						 * 			ils-req1-id => ils-req1-period,
						 * 			ils-req2-id...
						 * 		)
						 * 	),
						 * 	ci2-id = ...
						 * )
	 * @param string $requested_barcode (optional) If searched for physical item, this is the barcode matching the exact copy searched
	 */
	function displaySelectCIForItem($item_id, $all_possible_CIs, $selected_CIs, $CI_request_matches, $requested_barcode=null) {
		//get holding info for physical items
		$item = new reserveItem($item_id);
		if($item->isPhysicalItem()) {
			$zQry = Rd_Registry::get('root:ils');
			$holdingInfo = $zQry->getHoldings($item->getLocalControlKey(), 'control'); /*TODO this should be called in the manager... */
			$selected_barcode = $requested_barcode;
		}
		else {
			$holdingInfo = null;
			$selected_barcode = null;
		}
				
		//circ rules
		$circRules = new circRules();
?>
		<script type="text/javascript" language="JavaScript1.2" src="public/javascript/basicAJAX.js"></script>
		<script type="text/javascript" language="JavaScript1.2" src="public/javascript/request_ajax.js"></script>
				
		<script type="text/javascript">
			var current_form_block_id;
			
			function toggle_request_form(block_id) {
				//hide old selection
				if(document.getElementById(current_form_block_id)) {
					document.getElementById(current_form_block_id).style.display = 'none';
				}
				//show new selection
				if(document.getElementById(block_id)) {
					document.getElementById(block_id).style.display = '';
					//save new selection
					current_form_block_id = block_id;
				}				
			}
		</script>

<?php	
		//the way possible destination courses are displayed depends on request type		
		if(!empty($all_possible_CIs)):
			foreach($all_possible_CIs as $request_type=>$ci_data):
				//for RD requests, just show a simple header
				if($request_type == 'rd_requests'):
?>
		<br />
		<div class="headingCell1" style="width:30%">ReservesDirect courses requesting this item:</div>
		
<?php			elseif($request_type == 'ils_requests'): //for ILS requests, show a different header ?>

		<br />
		<div class="headingCell1" style="width:30%">ILS requests:</div>
		
<?php			endif; ?>

			<div class="headingCell1">
				<div style="width:60px; text-align:left; float:left;">&nbsp;</div>
				<div style="width:15%; text-align:left; float:left;">Course Number</div>
				<div style="width:30%; text-align:left; float:left;">Course Name</div>
				<div style="width:25%; text-align:left; float:left;">Instructor(s)</div>
				<div style="width:14%; text-align:left; float:left;">Term</div>
				<div style="width:55px; text-align:left; float:right; padding-right:5px;">Preview</div>
				<div style="clear:both;"></div>
			</div>
	
<?php
				if($request_type == 'rd_requests') {
					//the ci-data is the array of CIs
					//show those
					$selected_CIs = array(array_key_exists('ci', $_REQUEST) ? $_REQUEST['ci'] : '');
					self::displayCoursesForRequest($item_id, $ci_data, $selected_CIs, $CI_request_matches, $circRules, $holdingInfo, $selected_barcode);
				}
				elseif($request_type == 'ils_requests') {
					foreach($ci_data as $user_id=>$request_data) {
						//get instructor's name
						$instructor = new user($user_id);
						$instructor_name = $instructor->getName(false);
						
						//get a list of ILS courses requesting this item
						$ils_courses_string = '';
						foreach($request_data['requests'] as $ils_request_id) {
							//init ils request object
							$ils_request = new ILS_Request($ils_request_id);
							
							//add name to string
							$ils_courses_string .= '"<em>'.$ils_request->getCourseName().'</em>", ';
						}
						$ils_courses_string = rtrim($ils_courses_string, ', ');	//trim off the last comma
						
						//display header
?>
			<div style="padding:5px; border:1px solid black; background-color:#DFD8C6;">Item requested by <em><?php print($instructor_name); ?></em> for <em><?php print($ils_courses_string); ?></em></div>
<?php
						//display course list
						$selected_CIs = array($_REQUEST['ci']);
						self::displayCoursesForRequest($item_id, $request_data['ci_list'], $selected_CIs, $CI_request_matches, $circRules, $holdingInfo, $selected_barcode);
					}
				}
				
			endforeach;
?>			
		<p>
			<img src="public/images/astx-green.gif" alt="selected" width="15" height="15"> <span style="font-size:small;">= course requested this item</span> &nbsp;
			<img src="public/images/pencil.gif" width="24" height="20" /> <span style="font-size:small;">= active courses</span> &nbsp;
			<img src="public/images/activate.gif" width="24" height="20" /> <span style="font-size:small;">= new courses not yet in use</span> &nbsp;
			<img src="public/images/cancel.gif" width="24" height="20" /> <span style="font-size:small;">= courses canceled by the registrar</span> &nbsp;
		</p>
		<br />
		<br />

		<script type="text/javascript">
			request_ajaxify_forms();
		</script>
		
<?php	
		endif;
		
		//display ajax selectClass
		$mgr = new ajaxManager('lookupClass', 'storeRequest', 'addReserve', 'Continue', array('item_id'=>$item_id));
		$mgr->display();
	}
	
	
	/**
	 * Displays a list of CIs, along with special forms to submit ci-item combo for request
	 *
	 * @param unknown_type $course_instance_ids
	 * @param unknown_type $selected_CIs
	 * @param unknown_type $ci_request_matches
	 * @param unknown_type $propagated_data
	 * @param unknown_type $circRules
	 * @param unknown_type $holdingInfo
	 * @param unknown_type $selected_barcode
	 */
	static function displayCoursesForRequest($item_id, $course_instance_ids, $selected_CIs, $ci_request_matches, $circRules, $holdingInfo=null, $selected_barcode) {
		if (0 == count($course_instance_ids)) {
?>
		<div style="border:1px solid #666666;background-color:#fff;padding:1em;"> No courses currently requesting this item. </div>
<?php 			
			return;
		}

?>
		<div style="border-bottom:1px solid #666666;">		
<?php
		foreach ($course_instance_ids as $ci_id) {
			$ci = new courseInstance($ci_id);
			$ci->getCourseForUser();	//fetch the course object
			$ci->getInstructors();	//get a list of instructors
			
			//get crossListings
			try {
				$crossListings = $ci->getCrossListings();
				$crossListings_string = '';
				foreach($crossListings as $crossListing) {
					$crossListings_string .= ', '.$crossListing->displayCourseNo().' '.$crossListing->getName();
				}
				$crossListings_string = ltrim($crossListings_string, ', ');	//trim off the first comma
			} catch (Rd_Exception $e){
				if(410 == $e->getCode()) {
					$crossListings = array();
					$crossListings_string = '';
				} else {
					throw $e;	
				}
			}
	
			//see if there are request matches
			$requests = !empty($ci_request_matches[$ci->getCourseInstanceID()]) ? $ci_request_matches[$ci->getCourseInstanceID()] : null;
			
			//show status icon
			switch($ci->getStatus()) {
				case 'AUTOFEED':
					$edit_icon = '<img src="public/images/activate.gif" width="24" height="20" />';	//show the 'activate-me' icon
				break;
				case 'CANCELED':
					$edit_icon = '<img src="public/images/cancel.gif" alt="edit" width="24" height="20">';	//show the 'canceled' icon
				break;
				default:
					$edit_icon = '<img src="public/images/pencil.gif" alt="edit" width="24" height="20">';	//show the edit icon
				break;						
			}			
						
			$pre_select_ci_radio = '';
			//mark pre-selected courses
			if(in_array($ci->getCourseInstanceID(), $selected_CIs)) {
				$selected_img = '<img src="public/images/astx-green.gif" alt="selected" width="15" height="15">&nbsp;';
				if (sizeof($selected_CIs) == 1) 
				{
					//only one CI selected go ahead and select the radio button
					$pre_select_ci_radio = ' checked="CHECKED" ';
					$force_toggle = "<script language='JavaScript'>toggle_request_form('add_".$ci->getCourseInstanceID()."');</script>";
				} else {
                    $force_toggle ='';
                }
			} else {
				$selected_img = '';
                $force_toggle ='';
			}
						
			//display row
			$rowStyle = (empty($rowStyle) || ($rowStyle=='evenRow')) ? 'oddRow' : 'evenRow';	//set the style
			$rowStyle2 = (empty($rowStyle2) || ($rowStyle2=='oddRow')) ? 'evenRow' : 'oddRow';	//set the style
?>									
			<div class="<?php print($rowStyle); ?>" style="padding:5px;">					
				<div style="width: 30px; float:left; text-align:left;"><input id="select_ci_<?php print($ci->getCourseInstanceID()); ?>" name="ci" type="radio" value="<?php print($ci->getCourseInstanceID());?>" onclick="javascript: toggle_request_form('add_<?php print($ci->getCourseInstanceID());?>');" <?php print($pre_select_ci_radio); ?>/></div>
				<div style="width: 50px; float:left; text-align:left"><?php print($selected_img.$edit_icon); ?></div>
				<div style="width:15%; float:left;"><?php print($ci->course->displayCourseNo()); ?>&nbsp;</div>
				<div style="width:30%; float:left;"><?php print($ci->course->getName()); ?>&nbsp;</div>
				<div style="width:25%; float:left;"><?php print($ci->displayInstructors()); ?>&nbsp;</div>
				<div style="width:14%; float:left;"><?php print($ci->displayTerm()); ?>&nbsp;</div>
				<div style="width:55px; float:right;"><a href="javascript:openWindow('no_control=1&cmd=previewReservesList&ci=<?php print($ci->getCourseInstanceID()); ?>','width=800,height=600');">preview</a></div>
				<div style="clear:both;"></div>
<?php		if(!empty($crossListings_string)): ?>
				<div style=" margin-left:30px; padding-top:5px;"><em>Cross-listed As:</em> <small><?php print($crossListings_string); ?></small></div>
<?php		endif; ?>

				<div id="add_<?php print($ci->getCourseInstanceID()); ?>" style="display:none;">
					<?php self::displayCreateReserveForm($ci, $item_id, $circRules, $holdingInfo, $requests, $selected_barcode, $rowStyle2) ?>
				</div>
			</div>
		
<?php	
			print($force_toggle);
		}   ?>		
		</div>

<?php
	}
	
	
	/**
	 * Displays create-reserve/process-request form for the given ci and item
	 *
	 * @param unknown_type $ci
	 * @param unknown_type $item_id
	 * @param unknown_type $circRules
	 * @param unknown_type $holdingInfo
	 * @param unknown_type $requests
	 	 * $requests = array(
		 * 	ci1-id => array(
		 * 		'rd_request' => rd-req-id,
		 * 		'ils_requests' => array(
		 * 			ils-req1-id => ils-req1-period,
		 * 			ils-req2-id...
		 * 		)
		 * 	),
		 * 	ci2-id = ...
		 * )
	 * @param unknown_type $selected_barcode
	 * @param unknown_type $rowStyle
	 */
	static function displayCreateReserveForm($ci, $item_id, $circRules, $holdingInfo=null, $requests=null, $selected_barcode=null, $rowStyle='') {
		global $calendar;
		$ci->unCouch();
		$activeStatusId = Rd_Request_Status::getActiveStatusId();
		$inactiveStatusId = Rd_Request_Status::getInactiveStatusId();
		$item = new reserveItem($item_id);
?>
		<form name="create_reserve_form" method="post" action="index.php">
					<input type="hidden" name="cmd" value="storeRequest" />
					<input type="hidden" name="ci" value="<?php print($ci->getCourseInstanceID());?>" />
					<input type="hidden" name="item_id" value="<?php print($item_id); ?>" />
<?php
			//need to pass on request info (which requests are fullfilled by this item-ci combo)
			if(!empty($requests)) {
				//pass on RD request ID
				if(!empty($requests['rd_request'])) {
?>
					<input type="hidden" name="rd_request" value="<?php print($requests['rd_request']); ?>" />
<?php
				}
				if(!empty($requests['ils_requests'])) {
					foreach($requests['ils_requests'] as $ils_request_id=>$ils_requested_loan_period) {
?>
					<input type="hidden" name="ils_requests[]" value="<?php print($ils_request_id); ?>" />
<?php
					}
				}
			}
			if(!empty($ci_request_matches)) {
				if(!empty($ci_request_matches['rd_requests'])) {
					self::displayHiddenFields($ci_request_matches['rd_requests']);
				}
				foreach($ci_request_matches as $ci_request_match) {
					self::displayHiddenFields($ci_request_match);
				}
			}
			//self::displayHiddenFields($propagated_data); //#TODO wasn't defined...
?>
					<br />
					<table width="90%" border="0" cellpadding="3" cellspacing="0" class="borders <?php print($rowStyle); ?>" align="center">
						<tr>
							<td width="15%">&nbsp;</td>
							<td><br /><strong>Please enter reserve information for this course:</strong><br />&nbsp;</td>
						</tr>
						<tr>
							<td align="right"><strong>Set Status:</strong></td>
							<td>
								<input type="radio" name="reserve_status" id="reserve_status_active_<?php print($ci->getCourseInstanceID()); ?>" value="<?php print($activeStatusId); ?>" checked="true" />&nbsp;<span class="active">ACTIVE</span>
								<input type="radio" name="reserve_status" id="reserve_status_inactive_<?php print($ci->getCourseInstanceID()); ?>" value="<?php print($inactiveStatusId); ?>" />&nbsp;<span class="inactive">INACTIVE</span>
							</td>
						</tr>
						<tr>
							<td align="right"><strong>Active Dates:</strong></td>
							<td>
								<input type="text" id="reserve_activation_date_<?php print($ci->getCourseInstanceID()); ?>" name="reserve_activation_date" size="10" maxlength="10" value="<?php print($ci->getActivationDate()); ?>" style="margin-top:5px;" /> <?php print($calendar->getWidgetAndTrigger('reserve_activation_date_'.$ci->getCourseInstanceID(), $ci->getActivationDate())); ?> to <input type="text" id="reserve_expiration_date_<?php print($ci->getCourseInstanceID()); ?>" name="reserve_expiration_date" size="10" maxlength="10" value="<?php print($ci->getExpirationDate()); ?>" />  <?php print($calendar->getWidgetAndTrigger('reserve_expiration_date_'.$ci->getCourseInstanceID(), $ci->getExpirationDate())); ?>(YYYY-MM-DD)
							</td>
						</tr>
<?php		if($item->isPhysicalItem()): //the rest is only needed for physical items ?>						
<?php			
				$ils = Rd_Registry::get('root:ils');
				if(!empty($holdingInfo) && $ils->supportsReserveRecords()):	//have holding info, show physical copies ?>
						<tr>
							<td>&nbsp;</td>
							<td>
								<br />
								<span class="helperText">Below is a list of copies available through Sirsi.  <u>Select copies for which you wish to create a Sirsi 'on-reserve' record.</u>  Your selection(s) will have no impact on the Course Reserves reserves list.</span>
							</td>
						</tr>
						<tr>
							<td align="right"><strong>ILS Record:</strong></td>
							<td>
								<input type="checkbox" name="create_ils_record" value="yes" CHECKED />
								Create <?php print($ils->getName()); ?> Reserve Record
							</td>
						</tr>
						<tr>
							<td align="right"><strong>Requested Loan Period:</strong></td>
							<td><?php print( array_key_exists('loan_period', $_REQUEST) ? $_REQUEST['loan_period'] : ''); ?></td>
						</tr>
						<tr>
							<td align="right"><strong>Loan Period:</strong></td>
							<td>
								<select id="circRule_<?php print($ci->getCourseInstanceID()); ?>" name="circRule">
<?php		
				foreach($circRules->getCircRules() as $circRule):
					$rule = base64_encode(serialize($circRule));
					$display_rule = $circRule['circRule']." -- " . $circRule['alt_circRule'];
					$selected = $circRule['default'];
?>
									<option value="<?php print($rule); ?>" <?php print($selected); ?>><?php print($display_rule); ?></option>
<?php			endforeach; ?>
								</select>
<?php			if(!empty($requests['ils_requests'])):	//try to grab a requested loan period out of ils-requests data ?>
								&nbsp;(Requested loan period: <?php print(array_shift($requests['ils_requests'])); ?>)
<?php			endif; ?>
							</td>
						</tr>
						<tr>
							<td align="right" valign="top"><strong>Select Copy:</strong></td>
							<td>
<?php			
					foreach($holdingInfo as $phys_copy):
						$selected = ($phys_copy['bar'] == $selected_barcode) ? 'checked="checked"': '';
?>
						<input type="checkbox" name="physical_copy[]" value="<?php print(base64_encode(serialize($phys_copy))); ?>" <?php print($selected);?> />
						&nbsp;<?php print($phys_copy['type']); ?> | <?php print($phys_copy['library']); ?> | <?php print($phys_copy['loc']); ?> | <?php print($phys_copy['callNum']); ?> | <?php print($phys_copy['bar']); ?>
						<br />
<?php				endforeach; ?>
							</td>
						</tr>
<?php			
				endif;
			endif;
?>
						<tr>
							<td colspan="2" align="center">
								<br />
								<input type="submit" id="submit_store_item_<?php print($ci->getCourseInstanceID()); ?>" name="submit_store_item" value="Add Item to Class" style="margin-top:5px;" />
							</td>
						</tr>
					</table>					
				</form>
<?php
	}	
	
	public function addVideoItem($itemData, $ci) 
	{
		$u = Rd_Registry::get('root:userInterface');
		$itemData['times_pages2'] = null;
		if (array_key_exists('times_pages', $itemData)) {
			$times = explode(" to ", $itemData['times_pages']);
			$itemData['times_pages'] = $times[0];
			$itemData['times_pages2'] = $times[1];
		}

		$doc_types = $u->getAllDocTypeIcons(TRUE);
		
		//private user
		if (array_key_exists('selected_owner', $itemData)) {
			//get id
			$selectedOwnerId = $itemData['selected_owner'];
			$tmpUser = new user($selectedOwnerId);
			//get name
			$selectedOwnerName = $tmpUser->getName().' ('.$tmpUser->getUsername().')';
			unset($tmpUser);
		} else {
			$selectedOwnerName = '';
		}
		
		//deal with physical item source pre-select
		$addTypeSelect = array('euclid'=>'', 'personal'=>'');
		$existingFiles = Queue_Encoding::getUnassigned($u->getId());
		foreach ($existingFiles as $index=>$row) {
			$existingFiles[$index]['original_filename'] = "Use existing file, {$row['original_filename']}";
		}
		$model = array(
			'itemId' => false,
			'loanPeriod' => (
				array_key_exists('loan_period', $itemData) 
				? $itemData['loan_period'] 
				: ''
			),
			'personalItem' => !empty($selectedOwnerId),
			'selectedOwnerName' => $selectedOwnerName,
			'videoUrl' => '',
			'volumeTitle' => '',
			'author' => '',
			'performer' => '',
			'volumeTitle' => '',
			'source' => '',
			'timeMinutes' => '',
			'timeSeconds' => '',
			'volumeEdition' => '', 
			'isbn' => '',
			'issn' => '',
			'oclc' => '',
			'barcode' => '',
			'notes' => (
				array_key_exists('notes',$itemData) 
				? $itemData['notes'] 
				: array()
			),
			'hasUnclaimedUploads' => Queue_Encoding::userHasUnassigned($u->getId()),
			'fileList' => $existingFiles
		);
		$this->display('videoUploadForm', $model);
	}
	
	public function displayVideoUploadScripts()
	{
		$this->printStatic('videoUpload.js');	
	}
	
	public function displayVideoUploadDirections()
	{
		$this->printStatic('videoUploadDirections.html');	
	}
	
	public function displayRequestEstimates($model)
	{
		$this->display('estimates', $model);
	}
}

